package jin.si.com.utils;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * @description: 异步
 * @ClassName TestAsyncUtils
 * @Author MT
 * @Date 2021/5/25 15:16
 **/

@Component
@Async      //Async加载类上，所有的方法都是异步，加载在方法上当前方法是异步
public class TestAsyncUtils {

    public void getTask1(){
        try {
            Thread.sleep(4000);//睡眠四秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task1");

    }

    public void getTask2(){
        try {
            Thread.sleep(4000);//睡眠四秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task2");

    }

    public void getTask3(){
        try {
            Thread.sleep(4000);//睡眠四秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task3");

    }


    //Future 异步方式
    public Future<String> getTask4(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<String>("task4");
    }

    public Future<String> getTask5(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<String>("task5");
    }

    public Future<String> getTask6(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<String>("task6");
    }










}
